package com.provider.service3.controller;


import com.api.demo.entities.Dept;
import com.provider.service3.service.DeptService;
import com.provider.service3.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    @Autowired
    org.springframework.cloud.client.discovery.DiscoveryClient client;

    @RequestMapping(value = "/test")
    public String  test(){
        return "test";
    }


    @RequestMapping(value = "/add")
    public void add(Dept dept, PrintWriter out){
//        return deptService.add(dept);
        if(deptService.add(dept)){
            ResponseUtil.responseCode(out, 0);
        }
        ResponseUtil.responseCode(out, -1);
    }



    @RequestMapping(value = "/get/{id}")
    public void get(@PathVariable Long id, PrintWriter out){
        Map<String,Object> map = new HashMap<>();
        Dept dept = deptService.get(id);
        map.put("dept",dept);
        ResponseUtil.response(out, map);
//        return deptService.get(id);
    }


    @RequestMapping(value = "/list")
    public void list(PrintWriter out){
        Map<String,Object> map = new HashMap<>();
        map.put("dept", deptService.list());
        ResponseUtil.response(out, map);
    }

    @RequestMapping(value = "/discovery")
    public void discovery(PrintWriter out) {
        Map<String, Object> map = new HashMap<>();
        map.put("list", client.getServices());
//        List<ServiceInstance> serLIst = client.getInstances("microservicecloud-dept8010");
//        map.put("serList", serLIst);
        ResponseUtil.response(out, map);

    }


}
